<template>
  <div class="login-wrap">
    <div class="ms-title">账号登录</div>
    <div class="ms-login">
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="0px"
        class="demo-ruleForm"
      >
        <div v-if="errorInfo">
          <span>{{ errInfo }}</span>
        </div>
        <el-form-item prop="username">
          <el-input v-model="form.username" placeholder="用户名"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="form.password"
            show-password
            placeholder="密码"
          ></el-input>
        </el-form-item>
        <el-form-item prop="validate">
          <el-input
            v-model="form.validate"
            class="validate-code"
            placeholder="验证码"
          ></el-input>
          <div class="code" @click="refreshCode">
            <s-identify :identifyCode="identifyCode"></s-identify>
          </div>
        </el-form-item>
        <div class="login-btn">
          <el-button type="primary" @click="handleClick">登录</el-button>
          <el-button @click="toregister" style="margin: 5px 0px"
            >注册</el-button
          >
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'login',
  data() {
    return {
      identifyCodes: '1234567890',
      identifyCode: '',
      errorInfo: false,
      form: {
        username: '',
        password: '',
        validate: '',
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
        ],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
        validate: [
          {
            required: true,
            message: '请输入验证码',
            trigger: 'blur',
          },
        ],
      },
    };
  },
  mounted() {
    this.identifyCode = '';
    this.makeCode(this.identifyCodes, 4);
  },
  methods: {
    toregister() {
      this.$router.push('/register');
    },
    handleClick() {
      this.$refs['form'].validate(() => {
        if (this.form.username == '') {
          // 检验通
          // 检验不通过
          this.$message({
            type: 'error',
            message: '用户名不能为空',
          });
        } else if (this.form.password == '') {
          // 检验不通过
          this.$message({
            type: 'error',
            message: '密码不能为空',
          });
        } else if (this.form.validate == '') {
          // 检验不通过
          this.$message({
            type: 'error',
            message: '验证码不能为空',
          });
        } else {
          if (this.form.validate != this.identifyCode) {
            this.$message.error('验证码错误');
          } else {
            // 1. 发送ajax请求
            setTimeout(() => {
              this.$http
                .post('/user/login', {
                  user_name: this.form.username,
                  user_password: this.form.password,
                })
                .then((res) => {
                  console.log(res);
                  if (res.code == 0) {
                    this.$message.success('登录成功');
                    this.$cookies.set('username', res.result.data[0].user_name);
                    this.$cookies.set('usertype', res.result.data[0].user_type);
                    this.$router.push('/');
                    // sessionStorage.setItem("username", this.form.username);
                  } else {
                    this.$message.error('用户名或密码错误');
                  }

                  // 3. 跳转
                  // this.$router.push("/");
                });
            }, 1000);
          }
        }
      });
    },
    randomNum(min, max) {
      return Math.floor(Math.random() * (max - min) + min);
    },
    refreshCode() {
      this.identifyCode = '';
      this.makeCode(this.identifyCodes, 4);
    },
    makeCode(o, l) {
      for (let i = 0; i < l; i++) {
        this.identifyCode +=
          this.identifyCodes[this.randomNum(0, this.identifyCodes.length)];
      }
      console.log(this.identifyCode);
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style scoped>
.login-wrap {
  background-color: rgb(157, 166, 181);
  background-image: url(../../assets/v2-dc6e78ac3316ff07ccf1c4dac44800b1_r.jpg);
  background-size: 100% 100%;
  background-size: cover;
  background-repeat: no-repeat;
  position: relative;
  width: 100%;
  height: 100vh;
  margin: 0 auto;
}
.ms-title {
  position: absolute;
  top: 50%;
  width: 100%;
  margin-top: -230px;
  text-align: center;
  font-size: 30px;
  color: #fff;
}
.ms-login {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 300px;
  height: 240px;
  margin: -150px 0 0 -190px;
  padding: 40px;
  border-radius: 5px;
  background: #fff;
}
.ms-login span {
  color: red;
}
.login-btn {
  text-align: center;
}
.login-btn button {
  width: 100%;
  height: 36px;
}
.code {
  width: 112px;
  height: 35px;
  border: 1px solid #ccc;
  float: right;
  border-radius: 2px;
}
.validate-code {
  width: 136px;
  float: left;
}
.register {
  font-size: 14px;
  line-height: 30px;
  color: #999;
  cursor: pointer;
  float: right;
}
/* .el-form-item__label {
  margin-left: -20px;
}
.el-input__inner {
  margin-left: -20px;
} */
</style>
